ii=figure;


for site=1:size(proj_meta,2)
no_time_points = size(proj_meta(site).rd,2);

act=[];
type_sessions={'f','p','d'};
mean_act=[];
sem_act=[];
for tp=1:no_time_points
    act=[];
    for lyr=1:size(proj_meta(site).rd,1)
        act=[act; proj_meta(site).rd(lyr,tp).act];
    end
%     act=act(max(act')>2,:);
    temp=proj_meta(site).rd(lyr,tp).nbr_frames;
    temp=cumsum(temp);
    temp=[1 temp+1];
    velM = proj_meta(site).rd(lyr,tp).velM_smoothed;
    velP = proj_meta(site).rd(lyr,tp).velP_smoothed;

    for k = 1:length(type_sessions)
        ac_sess=find(strcmp(proj_meta(site).rd(lyr,tp).session,type_sessions(k))==1);
        indices=[];
        for kk=1:length(ac_sess)
            indices=[indices temp(ac_sess(kk)):temp(ac_sess(kk)+1)-1];
        end
        
        switch k
            case 1 % f
                velM_r = velM>0.001;
                start_perturb=strfind((proj_meta(site).rd(lyr,tp).ps_id>1),[0,1]);
                start_perturb=intersect(indices,start_perturb);
                ind_perturb=[];
                for kkk=1:length(start_perturb)
                    if start_perturb(kkk) > 5
                        if sum(velM_r(start_perturb(kkk)-5:start_perturb(kkk)+50))>52
                            ind_perturb = [ind_perturb start_perturb(kkk)-5:start_perturb(kkk)+50];
                        end
                    end
                end
                temp2=act(:,ind_perturb);
                temp2= reshape(temp2',56,prod(size(temp2))/56)';
                mean_act(tp,1:56)=mean(temp2,1);
        end
    end
end

figure(ii);
colors=[0 0 1;0 0 1;1 0 0;1 0 0;0 0 0;0 0 0;0 0 0;0 0 0;0 0 0;0 0 0;];
set(0,'DefaultAxesColorOrder',colors(1:no_time_points,:));
subplot(4,2,site);
plot(bsxfun(@rdivide,mean_act,mean(mean_act(:,1:5),2))')
% plot(mean_act');
ylim([0.95 1.06])
title([proj_meta(site).animal ' - ' num2str(proj_meta(site).ExpGroup(1))]);
end


